"use strict";
const mvc = require('cosjs.mvc');
/**
 * @name select
 * 选择角色
 */

/**
 * @name /
 * @param string uid 角色ID
 * 选择角色
 */
module.exports = async function () {
    let uid = this.get('uid');
    if (!uid) {
        return this.error('uid empty');
    }
    let role = this.model.mongo('role', this.sid, uid);
    let usrData = await role.get();

    if (!usrData) {
        return this.error('role[' + uid + '] not exist');
    }
    //DEBUG 临时的未成年验证系统
    // if (usrData['idcard'] == '') {
    //     return this.error('idCard not exist');
    // }
    // if (usrData['realName'] == '') {
    //     return this.error('realName not exist');
    // }
    //判断是否是未成年
    //判断当前的时间未成年是否能进游戏

    //补全消息
    let format = mvc.format('role');
    let modify = fillNewEle(usrData, format);
    if (modify != null) {
        await role.set(modify);
    }

    if (usrData.state == 404) {
        let blockM = this.model.mongo('block', this.sid);
        let blockData = await blockM.find({ 'uid': uid });
        if (blockData) {
            return this.error(blockData.msg);
        }
    }

    let sdata = { "uid": uid, "name": usrData['name'], "lv": usrData['lv'] || 1, "vip": usrData['vip'] || 0, "time": usrData['time'] || 0 }

    this.session.set(sdata);

    this.uid = uid;




    //每日登陆
    let lastTime = usrData['login'] || 0;
    let todayTime = this.library("time/today");
    usrData['login'] = Date.now();


    //在线统计
    let redis = this.model.redis("online");
    redis.set(this.sid, this.uid);


    //清理信息
    delete usrData.idCard;
    delete usrData.realName;
    if (lastTime >= todayTime) {
        return usrData;
    } else {
        //============================日常数据==================================
        let sumlog = usrData['sumlog'] || 0;
        let conlog = usrData['conlog'] || 0;
        usrData['sumlog'] = sumlog + 1;
        let yesterday = todayTime - 86400 * 1000;
        if (lastTime >= yesterday) {
            usrData['conlog'] = conlog + 1;
        }
        else {
            usrData['conlog'] = 1;
        }
        //DAU统计
        this.library("spirit/gzone", '/dau/', { "sid": this.sid, "time": usrData['time'] }).catch(err => {
            console.log("spirit/gzone", "/dau/", err)
        })
        //this.log('login',update.uid,update.get('time'),lastTime );
        let daily = this.model.mongo('daily', this.sid, this.uid);
        await daily.set(uid, "record.11", 1);
        await daily.set(uid, "login", 1);
        let record = this.model.mongo('record', this.sid, this.uid);
        // await record.set(uid,"login",1);
        this.updater.record.add(11, 1);
        await this.updater.save();
        let upsert = {};
        upsert['login'] = usrData.login;
        upsert['lastTime'] = usrData.login;
        upsert['sumlog'] = usrData.sumlog;
        upsert['conlog'] = usrData.conlog;
        await role.set(upsert);
        return usrData;
    }
};


function fillNewEle(json, config) {
    let modify = null;
    for (let k in config) {
        if (!(k in json)) {
            var v = config[k]['val'];
            if (v !== null) {
                json[k] = typeof v == 'object' ? JSON.clone(v) : v;
                if (modify == null) {
                    modify = {};
                }
                modify[k] = json[k];
            }
        }
    }
    return modify;
}
